package com.donger.web.admin.controller.app;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donger.business.modules.app.entity.AppVersion;
import com.donger.business.modules.app.service.AppVersionService;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import com.donger.common.web.baseweb.BaseController;
import com.donger.common.web.utils.ValidatorUtil;
import com.donger.web.admin.query.app.AppVersionQuery;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RequestMapping("/app/version")
@AllArgsConstructor
@RestController
public class VersionController extends BaseController {

    private final AppVersionService appVersionService;

    @GetMapping("/page")
    public Result page(Page<AppVersion> page, AppVersionQuery query) {
        return Res.ok(appVersionService.page(page, Wrappers.<AppVersion>lambdaQuery()
                .like(StrUtil.isNotBlank(query.getVersion()), AppVersion::getVersion, query.getVersion())
                .orderByDesc(AppVersion::getBuild)
        ));
    }


    @PostMapping("/save")
    public Result save(@RequestBody AppVersion appVersion) {
        ValidatorUtil.validateEntity(appVersion);
        appVersion.setCreateTime(LocalDateTime.now());
        appVersion.setUpdateTime(LocalDateTime.now());
        appVersionService.save(appVersion);


        return Res.ok();
    }


    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable Long id) {
        appVersionService.removeById(id);
        return Res.ok();
    }


}
